﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site/HeThongSpa.Master" AutoEventWireup="true" CodeBehind="DanhMucDichVu.aspx.cs" Inherits="HeThongSpa.QuanTriHeThong.DanhMuc.DanhMucDichVu" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Danh mục dịch vụ
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeaderContent" runat="server">
    <h1 style="font-weight: bold;">Danh mục dịch vụ</h1>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="MainContent" runat="server">
    <div class="box">
        <div class="box-header with-border">
            <h3 class="box-title" style="color: #E08E0B; font-size: 12px;">Lưu ý: Mã dịch vụ sẽ được tự động bỏ dấu, khoảng trắng và viết hoa toàn bộ.</h3>
            <div class="box-tools pull-right">
                <button type="button" class="btn btn-box-tool" data-widget="collapse">
                    <i class="fa fa-minus"></i>
                </button>
            </div>
        </div>
        <div class="box-body" style="padding-bottom: 0;">
            <div class="row">
                <div class="col-xs-12 col-sm-4 col-md-2">
                    <div class="form-group form-group-sm">
                        <label>
                            Mã dịch vụ&nbsp;<span style="color: #D73925;">*</span>
                        </label>
                        <asp:TextBox ID="txtMaDichVu" runat="server" CssClass="form-control" placeholder="Mã dịch vụ" MaxLength="50"></asp:TextBox>
                    </div>
                </div>
                <div class="col-xs-12 col-sm-8 col-md-4">
                    <div class="form-group form-group-sm">
                        <label>
                            Tên dịch vụ&nbsp;<span style="color: #D73925;">*</span>
                        </label>
                        <asp:TextBox ID="txtTenDichVu" runat="server" CssClass="form-control" placeholder="Tên dịch vụ" MaxLength="200"></asp:TextBox>
                    </div>
                </div>
                <div class="col-xs-12 col-sm-8 col-md-4">
                    <div class="form-group form-group-sm">
                        <label>
                            Nhóm dịch vụ&nbsp;<span style="color: #D73925;">*</span>
                        </label>
                        <asp:DropDownList ID="ddlNhomDichVu" runat="server" data-live-search="true" />
                    </div>
                </div>
                <div class="col-xs-12 col-sm-4 col-md-2">
                    <div class="form-group form-group-sm">
                        <label>
                            Giá dịch vụ (VNĐ)&nbsp;<span style="color: #D73925;">*</span>
                        </label>
                        <asp:TextBox ID="txtGiaDichVu" runat="server" CssClass="form-control numberpriceleft" placeholder="Giá dịch vụ" MaxLength="15"></asp:TextBox>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-12 col-sm-4 col-md-2">
                    <div class="form-group form-group-sm">
                        <label>
                            Số lần (lần)
                        </label>
                        <asp:TextBox ID="txtSoLanThucHien" runat="server" CssClass="form-control numberpriceleft" placeholder="Số lần thực hiện" MaxLength="2"></asp:TextBox>
                    </div>
                </div>
                <div class="col-xs-12 col-sm-4 col-md-2">
                    <div class="form-group form-group-sm">
                        <label>
                            Thời gian (phút/lần)
                        </label>
                        <asp:TextBox ID="txtThoiGianThucHien" runat="server" CssClass="form-control numberpriceleft" placeholder="Thời gian thực hiện" MaxLength="5"></asp:TextBox>
                    </div>
                </div>
                <div class="col-xs-12 col-sm-4 col-md-2">
                    <div class="form-group form-group-sm">
                        <label>
                            Thuế (%)
                        </label>
                        <asp:DropDownList ID="ddlThue" runat="server" data-live-search="true" />
                    </div>
                </div>
            </div>
        </div>
        <div class="box-footer">
            <asp:Button ID="btnLuu" CssClass="btn btn-primary btn-sm btn-flat" runat="server" Text="Lưu"
                OnClientClick=" validationform() " OnClick="btnLuu_Click" />
            <asp:Button ID="btnXoa" CssClass="btn btn-danger btn-sm btn-flat" runat="server" Text="Xóa"
                OnClientClick=" return btnXoa_showconfirm(this.id); " OnClick="btnXoa_Click" />
            <asp:Button ID="btnTimKiem" CssClass="btn btn-success btn-sm btn-flat" runat="server" Text="Tìm kiếm"
                OnClientClick=" $('#form_Main').validate().cancelSubmit = true; " OnClick="btnTimKiem_Click" />
            <asp:Button ID="btnNhapLai" CssClass="btn btn-warning btn-sm btn-flat" runat="server" Text="Nhập lại"
                OnClientClick=" $('#form_Main').validate().cancelSubmit = true; " OnClick="btnNhapLai_Click" />
        </div>
    </div>
    <div class="box">
        <div class="box-header with-border">
            <h3 class="box-title"></h3>
            <div class="box-tools pull-right">
                <button type="button" class="btn btn-box-tool" data-widget="collapse">
                    <i class="fa fa-minus"></i>
                </button>
            </div>
        </div>
        <div class="box-body" style="padding-bottom: 0;">
            <div class="table-responsive">
                <asp:GridView ID="gvDanhMuc" runat="server" AutoGenerateColumns="False" Width="100%" CssClass="table table-bordered table-condensed"
                    OnRowCreated="gvDanhMuc_OnRowCreated" DataKeyNames="MaDichVu" ShowHeaderWhenEmpty="True">
                    <EmptyDataTemplate>
                        <div class="row text-center">
                            <label>Không có dữ hiện hiển thị</label>
                        </div>
                    </EmptyDataTemplate>
                    <Columns>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:CheckBox ID="checkall_id" runat="server" ToolTip="Chọn tất cả" onclick="javascript:checkallClick(this);" />
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="check_id" runat="server" />
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                        <asp:BoundField DataField="RowNumber" HeaderText="STT">
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:BoundField>
                        <asp:BoundField DataField="TenDichVu" HeaderText="Tên dịch vụ"></asp:BoundField>
                        <asp:BoundField DataField="TenNhomDichVu" HeaderText="Nhóm dịch vụ"></asp:BoundField>
                        <asp:BoundField DataField="GiaDichVu" HeaderText="Giá dịch vụ" DataFormatString="{0:n0}">
                            <ItemStyle HorizontalAlign="Right"></ItemStyle>
                        </asp:BoundField>
                        <asp:BoundField DataField="SoLanThucHien" HeaderText="Số lần thực hiện" DataFormatString="{0:n0}">
                            <ItemStyle HorizontalAlign="Right"></ItemStyle>
                        </asp:BoundField>
                        <asp:BoundField DataField="ThoiGianThucHien" HeaderText="Thời gian thực hiện" DataFormatString="{0:n0}">
                            <ItemStyle HorizontalAlign="Right"></ItemStyle>
                        </asp:BoundField>
                        <asp:BoundField DataField="Thue" HeaderText="Thuế">
                            <ItemStyle HorizontalAlign="Right"></ItemStyle>
                        </asp:BoundField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="btnHoaHong" runat="server" ToolTip="Hoa hồng" CssClass="btn btn-primary btn-xs btn-flat"
                                    madichvu='<%# Eval("MaDichVu") %>' OnClientClick="hoahongdichvu(this)" href="javascript:void(0)">
                                <i class="glyphicon glyphicon-usd"></i>
                                </asp:LinkButton>
                                <asp:LinkButton ID="btnSua" runat="server" ToolTip="Sửa" CssClass="btn btn-success btn-xs btn-flat"
                                    CommandArgument='<%# Eval("MaDichVu") %>' OnClick="btnSua_Click" OnClientClick=" $('#form_Main').validate().cancelSubmit = true; ">
                                <i class="glyphicon glyphicon-edit"></i>
                                </asp:LinkButton>
                                <asp:LinkButton ID="btnDelete" runat="server" ToolTip="Xóa" CssClass="btn btn-danger btn-xs btn-flat"
                                    CommandArgument='<%# Eval("MaDichVu") %>' OnClick="btnDelete_Click" OnClientClick=" return showconfirm(this.id); ">
                                <i class="glyphicon glyphicon-trash"></i>
                                </asp:LinkButton>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
            <div class="row">
                <div class="col-xs-12 col-sm-12 col-md-12">
                    <asp:DataList RepeatDirection="Horizontal" runat="server" ID="dlPager" OnItemCommand="dlPager_ItemCommand"
                        CssClass="tbPage">
                        <ItemTemplate>
                            <asp:LinkButton Enabled='<%#Eval("Enabled") %>' runat="server" ID="lnkPageNo" Text='<%#Eval("Text") %>'
                                CommandArgument='<%#Eval("Value") %>' CommandName="PageNo">
                            </asp:LinkButton>
                        </ItemTemplate>
                    </asp:DataList>
                </div>
            </div>
        </div>
    </div>
    <asp:HiddenField ID="hfTimKiem" runat="server" Value="false" />
    <asp:HiddenField ID="hfPageIndex" runat="server" Value="1" />
    <script type="text/javascript">
        var TotalChkBx,
            Counter,
            madichvu = "";

        $(function () {
            $("#li_danhmuc").addClass("active");
            $("#li_danhmuc_danhmucdichvu").addClass("active");
            TotalChkBx = parseInt("<%= gvDanhMuc.Rows.Count %>");
            Counter = 0;
            $("#div_hoahongdichvu").on("hide", function () {
                madichvu = "";
            });
        });

        function validationform() {
            $("#<%= txtMaDichVu.ClientID %>").rules("add", {
                required: true,
                messages: {
                    required: errempty
                }
            });
            $("#<%= txtTenDichVu.ClientID %>").rules("add", {
                required: true,
                messages: {
                    required: errempty
                }
            });
            $("#<%= ddlNhomDichVu.ClientID %>").rules("add", {
                required: true,
                messages: {
                    required: errempty
                }
            });
            $("#<%= txtGiaDichVu.ClientID %>").rules("add", {
                required: true,
                messages: {
                    required: errempty
                }
            });
        }

        function checkallClick(checkBox) {
            var targetBaseControl = document.getElementById("<%= gvDanhMuc.ClientID %>");
            var targetChildControl = "check_id";
            var inputs = targetBaseControl.getElementsByTagName("input");
            for (var n = 0; n < inputs.length; ++n)
                if (inputs[n].type === "checkbox" && inputs[n].id.indexOf(targetChildControl, 0) >= 0)
                    inputs[n].checked = checkBox.checked;
            Counter = checkBox.checked ? TotalChkBx : 0;
        }

        function checkClick(checkBox, hCheckBox) {
            var headerCheckBox = document.getElementById(hCheckBox);
            if (checkBox.checked && Counter < TotalChkBx) Counter++;
            else if (Counter > 0) Counter--;
            if (Counter < TotalChkBx) headerCheckBox.checked = false;
            else if (Counter === TotalChkBx) headerCheckBox.checked = true;
        }

        var confirmed = false;

        function btnXoa_showconfirm(controlId) {
            if (Counter === 0) {
                bootbox.alert("Bạn cần chọn bản ghi trước khi xóa!");
            } else {
                if (confirmed) {
                    return true;
                }
                bootbox.confirm("Bạn có chắc chắn xóa?", function (result) {
                    if (result) {
                        if (controlId != null) {
                            var controlToClick = document.getElementById(controlId);
                            if (controlToClick != null) {
                                confirmed = true;
                                $("#form_Main").validate().cancelSubmit = true;
                                controlToClick.click();
                                confirmed = false;
                            }
                        }
                    }
                });
            }
            return false;
        }

        function showconfirm(controlId) {
            if (confirmed) return true;
            bootbox.confirm("Bạn có chắc chắn xóa?", function (result) {
                if (result) {
                    if (controlId != null) {
                        var controlToClick = document.getElementById(controlId);
                        if (controlToClick != null) {
                            confirmed = true;
                            $("#form_Main").validate().cancelSubmit = true;
                            controlToClick.click();
                            confirmed = false;
                        }
                    }
                }
            });
            return false;
        }

        function hoahongdichvu(taga) {
            madichvu = $(taga).attr("madichvu");
            $("#tb_hoahongdichvu").bootstrapTable("destroy");
            $("#tb_hoahongdichvu").bootstrapTable({
                classes: "table table-condensed",
                height: 450,
                data: PhanTramHoaHongDichVu.PhanTramHoaHongDichVu_SelectByMaDichVu(madichvu),
                cache: false,
                search: true,
                searchAlign: "right",
                formatShowingRows: function () {
                    return "";
                },
                columns: [
                    {
                        field: "TenChucVu",
                        title: "Chức vụ"
                    },
                    {
                        field: "PhanTramHoaHong",
                        title: "Hoa hồng (%)",
                        width: 150,
                        formatter: function (value, row, index) {
                            return "<div class='form-group form-group-sm' style='margin-bottom: 0;'><input type='text' class='form-control' value='" + value + "' macv='" + row.MaChucVu + "' /></div>";
                        }
                    }
                ]
            });
            $("#tb_hoahongdichvu > tbody :input[type='text']").bind("keydown", function (e) {
                var n = $("#tb_hoahongdichvu > tbody :input[type='text']").length;
                if (e.which === 40) {
                    e.preventDefault();
                    var nextIndex = $("#tb_hoahongdichvu > tbody :input[type='text']").index(this) + 1;
                    if (nextIndex < n) {
                        $("#tb_hoahongdichvu > tbody :input[type='text']")[nextIndex].focus();
                    }
                } else if (e.which === 38) {
                    e.preventDefault();
                    var prevIndex = $("#tb_hoahongdichvu > tbody :input[type='text']").index(this) - 1;
                    if (prevIndex < n && prevIndex >= 0) {
                        $("#tb_hoahongdichvu > tbody :input[type='text']")[prevIndex].focus();
                    }
                } else if (e.which === 13) {
                    e.preventDefault();
                    $("#btnLuuHoaHong").focus();
                }
            });
            $("#div_hoahongdichvu").modal({
                show: true,
                focusOn: $("#tb_hoahongdichvu > tbody :input[type='text']:first")
            });
        }

        function luuhoahong() {
            PhanTramHoaHongDichVu.DeleteByMaDichVu(madichvu);
            $("#tb_hoahongdichvu > tbody :input[type='text']").each(function () {
                if ($(this).val() === "0") return;
                PhanTramHoaHongDichVu.Insert(madichvu, $(this).attr("macv"), $(this).val(), "<%= Session["username"]%>");
            });
        }

        var PhanTramHoaHongDichVu = {
            ArrJson: [],
            Insert: function (madichvu, machucvu, phantramhoahongdichvu, taikhoan) {
                $.ajax({
                    type: "POST",
                    url: "/Webservice/QuanTriHeThong/DanhMuc/wsPhanTramHoaHongDichVu.asmx/Insert",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: "{'madichvu': '" + madichvu.trim() + "', 'machucvu': '" + machucvu.trim() + "'" +
                         ", 'phantramhoahongdichvu': " + parseFloat(phantramhoahongdichvu.trim()) + "" +
                         ", 'taikhoan': '" + taikhoan.trim() + "'}",
                    async: false,
                    success: function () {
                    },
                    error: function () {
                        bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
                    }
                });
            },
            DeleteByMaDichVu: function (madichvu) {
                $.ajax({
                    type: "POST",
                    url: "/Webservice/QuanTriHeThong/DanhMuc/wsPhanTramHoaHongDichVu.asmx/DeleteByMaDichVu",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: "{'madichvu': '" + madichvu.trim() + "'}",
                    async: false,
                    success: function () {
                    },
                    error: function () {
                        bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
                    }
                });
            },
            PhanTramHoaHongDichVu_SelectByMaDichVu: function () {
                $.ajax({
                    type: "POST",
                    url: "/Webservice/QuanTriHeThong/DanhMuc/wsPhanTramHoaHongDichVu.asmx/PhanTramHoaHongDichVu_SelectByMaDichVu",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: "{'madichvu': '" + madichvu + "'}",
                    async: false,
                    success: function (n) {
                        PhanTramHoaHongDichVu.ArrJson = $.parseJSON(n.d);
                    },
                    error: function () {
                        bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
                    }
                });
                return PhanTramHoaHongDichVu.ArrJson;
            }
        }
    </script>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="FormContent" runat="server">
    <!-- Modal hoa hồng của dịch vụ -->
    <div id="div_hoahongdichvu" class="modal fade" tabindex="-1" role="dialog" data-backdrop="static"
        data-focus-on="" data-width="40%" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" style="line-height: 1;">
                        <span>&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title" id="myModalLabel">Hoa hồng dịch vụ cho từng chức vụ</h4>
                </div>
                <div class="modal-body">
                    <div class="table-responsive" style="margin-top: -10px;">
                        <table id="tb_hoahongdichvu">
                        </table>
                    </div>
                </div>
                <div class="modal-footer">
                    <input id="btnLuuHoaHong" type="button" value="Lưu" class="btn btn-primary btn-sm btn-flat" onclick="luuhoahong()" />
                </div>
            </div>
        </div>
    </div>
</asp:Content>
